package com.java.mall_imooc.controller;

import com.java.mall_imooc.common.utils.PageUtils;
import com.java.mall_imooc.common.utils.R;
import com.java.mall_imooc.entity.Shipping;
import com.java.mall_imooc.form.ShippingForm;
import com.java.mall_imooc.interceoter.LoginInterceptor;
import com.java.mall_imooc.service.ShippingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Map;


/**
 * @author jl
 * @since 2020-03-14 23:41:10
 */
@RestController
@RequestMapping("/shipping")
public class ShippingController {
    @Autowired
    private ShippingService shippingService;

    /**
     * 分页查询shipping
     * 当前页码page、每页显示记录数limit
     */
    @GetMapping
    public R list(Shipping shipping, @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        if (pageNum < 1) pageNum = 1;
        if (pageSize > 100) pageSize = 100;
        PageUtils page = shippingService.queryPage(shipping, pageNum, pageSize);
        return R.ok(page);
    }


    /**
     * 根据主键查询shipping
     */
    @GetMapping("{shippingId}")
    public R info(@PathVariable("shippingId") Integer shippingId) {
        Shipping shipping = shippingService.getById(shippingId);

        return R.ok(shipping);
    }

    /**
     * 新增shipping
     */
    @PostMapping
    public R<Map<String,Integer>> add(@RequestBody @Valid ShippingForm shippingForm) {
        return shippingService.add(LoginInterceptor.getUserInfo().getUserId(), shippingForm);
    }

    /**
     * 修改shipping
     */
    @PutMapping("{shippingId}")
    public R update(@PathVariable("shippingId")Integer shippingId, @RequestBody @Valid ShippingForm shippingForm) {
        shippingService.update(LoginInterceptor.getUserInfo().getUserId(), shippingId, shippingForm);

        return R.ok();
    }

    /**
     * 删除shipping
     */
    @DeleteMapping("{shippingId}")
    public R delete(@PathVariable("shippingId") Integer shippingId) {
        shippingService.delete(LoginInterceptor.getUserInfo().getUserId(), shippingId);
        return R.ok();
    }

}
